package io.renren.modules.blog.controller;

import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.annotation.LogOperation;
import io.renren.common.utils.Result;
import io.renren.common.page.PageData;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.blog.dao.BlogCategoryDao;
import io.renren.modules.blog.dto.BlogCategoryDto;
import io.renren.modules.blog.service.BlogCategoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (BlogCategory)表控制层
 *
 * @author chy
 * @since 2024-05-30 16:26:48
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blogCategory")
@Api(tags = "")
public class BlogCategoryController {
    /**
     * 服务对象
     */
    private final BlogCategoryService blogCategoryService;

    /**
     * 分页查询
     *
     * @param params 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public Result<PageData<BlogCategoryDto>> queryByPage(@RequestParam Map<String, Object> params) {
        return new Result<PageData<BlogCategoryDto>>().ok(blogCategoryService.page(params));
    }

    /**
     * 全量查询
     *
     * @param params 筛选条件
     * @return 查询结果
     */
    @GetMapping("/getList")
    public Result<List<BlogCategoryDto>> getList(@RequestParam Map<String, Object> params) {
        return new Result<List<BlogCategoryDto>>().ok(blogCategoryService.getList(params));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("详情")
    @LogOperation("保存")
    @GetMapping("{id}")
    public Result<BlogCategoryDto> get(@PathVariable("id") Long id) {
        BlogCategoryDto data = blogCategoryService.get(id);
        return new Result<BlogCategoryDto>().ok(data);
    }

    /**
     * 新增数据
     *
     * @param blogCategoryDto 实体
     */
    @ApiOperation("保存")
    @LogOperation("保存")
    @PostMapping
    public Result save(@RequestBody BlogCategoryDto blogCategoryDto) {
        //效验数据
        ValidatorUtils.validateEntity(blogCategoryDto, AddGroup.class, DefaultGroup.class);

        blogCategoryService.save(blogCategoryDto);

        return new Result();
    }

    /**
     * 编辑数据
     *
     * @param blogCategoryDto 实体
     */

    @ApiOperation("修改")
    @LogOperation("修改")
    @PutMapping
    public Result update(@RequestBody BlogCategoryDto blogCategoryDto) {
        //效验数据
        ValidatorUtils.validateEntity(blogCategoryDto, UpdateGroup.class, DefaultGroup.class);

        blogCategoryService.update(blogCategoryDto);

        return new Result();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除")
    @LogOperation("删除")
    @DeleteMapping("{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Long id) {
        return new Result<Boolean>().ok(blogCategoryService.deleteById(id));
    }

}

